package com.tsmti.lab.controller.system;

import com.tsmti.core.common.constant.Constants;
import com.tsmti.core.common.controller.BaseCrudController;
import com.tsmti.core.common.model.AjaxJson;
import com.tsmti.core.util.BeanUtils;
import com.tsmti.core.util.ObjectUtils;
import com.tsmti.shiro.annotation.RequiresPathPermission;
import com.tsmti.task.entity.ScheduleJob;
import com.tsmti.task.service.ScheduleJobService;
import org.quartz.CronExpression;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Package：com.tsmti.lab.controller.system
 * @Class：ScheduleJobController
 * @Description： TODO
 * @Author：zcw
 * @Date：Created in 2018/5/28 11:42
 * @Company:
 * @Version：
 * @Modified By:
 */
@Controller
@RequestMapping("/system/task/schedulejob")
@RequiresPathPermission("task:schedulejob")
public class ScheduleJobController extends BaseCrudController<ScheduleJob, String> {

    @Autowired
    private ScheduleJobService scheduleJobService;

    @RequestMapping(value = "/changeJobStatus", method = RequestMethod.POST)
    @ResponseBody
    public AjaxJson changeJobStatus(ScheduleJob scheduleJob, HttpServletRequest request,
                                    HttpServletResponse response) {
        String cmd = request.getParameter("cmd");
        String label = "停止";
        if (Constants.START.equals(cmd)) {
            label = "启动";
        } else {
            label = "停止";
        }
        AjaxJson ajaxJson = AjaxJson.success("任务" + label + "成功");
        try {
            scheduleJobService.changeStatus(scheduleJob.getId(), cmd);
        } catch (Exception e) {
            e.printStackTrace();
            ajaxJson = AjaxJson.fail("任务" + label + "失败" + e.getMessage());
        }
        return ajaxJson;
    }

    @RequestMapping(value = "/updateCron", method = RequestMethod.POST)
    @ResponseBody
    public AjaxJson updateCron(ScheduleJob scheduleJob, HttpServletRequest request,
                               HttpServletResponse response) {
        AjaxJson ajaxJson = AjaxJson.success("任务更新成功");
        try {
            scheduleJobService.updateCron(scheduleJob.getId());
        } catch (Exception e) {
            e.printStackTrace();
            ajaxJson = AjaxJson.fail("任务更新失败" + e.getMessage());
        }
        return ajaxJson;
    }

    @RequestMapping(value = "/saveScheduleJob", method = RequestMethod.POST)
    @ResponseBody
    public AjaxJson saveScheduleJob(ScheduleJob scheduleJob, HttpServletRequest request, HttpServletResponse response) {
        AjaxJson ajaxJson =  AjaxJson.success("保存成功");

        if (!CronExpression.isValidExpression(scheduleJob.getCronExpression())) {
            ajaxJson = AjaxJson.fail("cron表达式格式不对");
            return ajaxJson;
        }
        try {
            if (ObjectUtils.isNullOrEmpty(scheduleJob.getId())) {
                baseService.save(scheduleJob);
            } else {
                // FORM NULL不更新
                ScheduleJob oldEntity = baseService.get(scheduleJob.getId());
                BeanUtils.copyBeanNotNull2Bean(scheduleJob, oldEntity);
                baseService.update(oldEntity);
            }
        } catch (Exception e) {
            e.printStackTrace();
            ajaxJson = AjaxJson.fail("保存失败"+e.getMessage());
        }
        return ajaxJson;
    }

}
